package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.baseConfig.queryBean.MeterModelQueryBean;
import com.koron.css2.baseConfig.vo.MeterModelVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.meterService.bean.query.MsInfoQueryBean;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 表务中心 水表查询
 * 
 * @author 周利斌
 *
 */
public class MsInfoList implements ServerInterface {

	private static Logger logger = LogManager.getLogger(MsInfoList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);

		try {
			MsInfoQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsInfoQueryBean.class);
			MsInfoMapper mapper = factory.getMapper(MsInfoMapper.class);
			if(bean.getIsMsChg() != null && bean.getIsMsChg() == 1) {
				//仅针对换表查询在库水表列表
				MsChgPlanDetailMapper msChgPlanDetailMapper = factory.getMapper(MsChgPlanDetailMapper.class);
				List<String> newMeterNos = msChgPlanDetailMapper.selectNewMeterAndProceddNotEnd();
				bean.setNewMeterNos(newMeterNos);
			}
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<MsInfoDto> list = mapper.selectMsInfoListByFuzzy(bean);
			list.stream().forEach(t -> {setMsInfo(factory, t,userInfo);});
			info.setData(new PageInfo<MsInfoDto>(list));

		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			logger.error(e.getMessage(), e);
		}
		return info;
	}

	public List<String> ListIntegration(List<String> ...lists){
		return Arrays.stream(lists).flatMap(List::stream).collect(Collectors.toList());
	}


	public MsInfoDto setMsInfo(SessionFactory factory, MsInfoDto t,UserInfoBean userInfo) {
		Map<String, String> meterStatus = Tools.mapDicByCode(factory, "MMS");
		Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF");
		Map<String, String> commOperators = Tools.mapDicByCode(factory, "TXS");
		Map<String, String> transmittalModel = Tools.mapDicByCode(factory, "TPW");
		Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
		Map<String, String> meterPatterns = Tools.mapDicByCode(factory, "MMP");
		Map<String, String> meterSensors = Tools.mapDicByCode(factory, "MSS"); //水表传感器
		Map<String, String> protocols = Tools.mapDicByCode(factory, "XXGY"); //下行规约
		//MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		//List<MeterFactoryVO> meterFactorys = meterFactoryMapper.selectMeterFactoryList(new MeterFactoryQueryBean());
		//List<BaseMeterModelBean> baseMeterModels = meterModelMapper.selectMeterModelList(new BaseMeterModelQueryBean());
		//List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		if(StringUtils.isNotBlank(t.getMeterBore())) {
			MeterBoreBean boreBean =  meterBoreMapper.selectMeterBoreById(t.getMeterBore());
			// 水表口径
			t.setMeterBoreName(boreBean == null ?t.getMeterBore() :boreBean.getBoreName());
		}
			//传输方式
		if (StringUtils.isNotBlank(t.getTransmittalModel())) {
			t.setTransmittalModelName(transmittalModel.get(t.getTransmittalModel()));
		}
		if (StringUtils.isNotBlank(t.getMeterStatus())) {
			// 水表状态
			t.setMeterStatusName(meterStatus.get(t.getMeterStatus()));
		}
		if (StringUtils.isNotBlank(t.getMeterForm())) {
			// 水表形态
			t.setMeterFormName(meterForms.get(t.getMeterForm()));
		}
		if (StringUtils.isNotBlank(t.getMeterType())) {
			// 水表类型
			t.setMeterTypeName(meterTypes.get(t.getMeterType()));
		}
		if (StringUtils.isNotBlank(t.getCommOperator())) {
			// 通讯商
			t.setCommOperatorName(commOperators.get(t.getCommOperator()));
		}
		if (StringUtils.isNotBlank(t.getMeterPattern())) {
			// 水表型式
			t.setMeterPatternName(meterPatterns.get(t.getMeterPattern()));
		}
		if (StringUtils.isNotBlank(t.getMeterSensor())) {
			// 水表传感器
			t.setMeterSensorName(meterSensors.get(t.getMeterSensor()));
		}
		if(StringUtils.isNotBlank(t.getProtocol())) {
			//下行规约
			t.setProtocolName(protocols.get(t.getProtocol()));
		}
		if(t.getValveControl() != null) {
			if(1 == t.getValveControl())
				t.setValveControlName("是");
			if(0 == t.getValveControl())
				t.setValveControlName("否");
		}
		//水表厂商
        MeterFactoryMapper meterFactoryMapper=factory.getMapper(MeterFactoryMapper.class);
        MeterFactoryBean factoryBean =  meterFactoryMapper.selectMeterFactoryById(t.getMeterFactory());     
		// 水表厂家
		t.setMeterFactoryName(factoryBean==null?t.getMeterFactory():factoryBean.getFactoryName());
		//水表型号
		MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
		List<MeterModelVO> modelList = mapper.selectMeterModelList(new MeterModelQueryBean());
		if(StringUtils.isNotBlank(t.getMeterModel())) {
			t.setMeterModelName(modelList.stream().filter(y-> y.getId().equals(t.getMeterModel())).findFirst().orElse(new MeterModelVO()).getModelName());
			if(StringUtils.isBlank(t.getMeterModelName()))
				t.setMeterModelName(t.getMeterModel());
		}
		if (StringUtils.isNotBlank(t.getDepartBelong())) {
			//营业区域
			List<SelectVO> businessAreaList = factory.getMapper(BusinessAreaMapper.class).select(null, userInfo.getCurWaterCode());
//			// 营业网点
//			Optional<BaseBusinessBranchBean> optional = businessBranchs.stream().filter(o -> Objects.equals(o.getId(), t.getDepartBelong())).findFirst();
//			t.setDepartBelongName(optional.orElse(new BaseBusinessBranchBean()).getBranchName());
			//营业所
			//BusinessAbodeMapper Businessmapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
			//获取下拉框
			//List<SelectVO> businesslist = Businessmapper.businessAbodeSelect(userInfo.getCurWaterCode());
			//营业所
			for (SelectVO businessArea : businessAreaList) {
				if (businessArea.getId().equals(t.getDepartBelong())) {
					t.setDepartBelongName(businessArea.getName());
					break;
				}
			}
		}
		return t;
	}
}
